/* This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.gnarly.model;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import com.gnarly.FloodProtection;
import com.gnarly.Main;

/**
 * Client.java
 * 
 * Handles a client that is connected but not logged in
 * 
 * @author gnarly
 */
public class Client implements Runnable {
	
	private Socket socket;
	
	private BufferedReader in;
	private PrintWriter out;
	
	private String host;
	
	private boolean disconnected;
	private boolean initialized;
	
	public Client(final Socket socket) {
		this.socket = socket;
		host = socket.getInetAddress().getHostName();
	}
	
	public void run() {
		try {
			while (!disconnected) {
				in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				out = new PrintWriter(socket.getOutputStream());
				out.println("Gnarlys Telnet Server Framework.");
				out.println("Administration Login:");
				out.flush();
				boolean loggedIn = false;
				do {
					do {
						out.println();
						out.print("Username: ");
						out.flush();
					} while (!in.readLine().equals(Main.getUsername()));
					out.println();
					out.print("Password: ");
					out.flush();
					if (in.readLine().equals(Main.getPassword())) {
						loggedIn = true;
					}
				} while (!loggedIn);
				out.println();
				out.println("Authentication Sucessful!");
				out.println("Hello there, welcome to Gnarlys Telnet Server Framework!");
				out.println();
				out.flush();
				final Console console = new Console(socket.getInputStream(), socket.getOutputStream(), this);
				console.run();
			}
		} catch (final Exception e) {
			disconnected = true;
		}
	}
	
	public void initialize() {
		// TODO invoked when this client is first added (first tick)
	}
	
	public void tick() {
		// TODO incoked every 500ms (oh god)
	}
	
	public void destruct() {
		if(socket == null)
			return;
		try {
			disconnected = true;
			if(in != null)
				in.close();
			if(out != null)
				out.close();
			socket.close();
			socket = null;
			in = null;
			out = null;
			synchronized(this) {
				notify();
			}
		} catch(IOException e) {
			e.printStackTrace();
		}
		FloodProtection.remove(host);
	}
	
	public void setDisconnected(final boolean disconnected) {
		this.disconnected = disconnected;
	}

	public boolean isDisconnected() {
		return disconnected;
	}

	public void setInitialized(boolean initialized) {
		this.initialized = initialized;
	}

	public boolean isInitialized() {
		return initialized;
	}

}
